//
//  WayModelDateView.m
//  WayService
//
//  Created by WillyZhao on 16/8/12.
//  Copyright © 2016年 Johnny. All rights reserved.
//

#import "WayModelDateView.h"

@interface WayModelDateView () {
    UIView *maskView;
    NSString *str_date;
}

@end

@implementation WayModelDateView

-(instancetype)initWithFrame:(CGRect)frame AndStyle:(WayModelPickerViewStyle)style WithData:(NSArray *)dataSource{
    if (self = [super initWithFrame:frame]) {
        self.style = style;
        self.backgroundColor = [UIColor whiteColor];
        
        _pickerView = [[WayModelPickerView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 50) AndDataSource:dataSource WithStyle:style];
        _pickerView.block = ^(NSString *str) {
            str_date = [NSString stringWithFormat:@"%@",str];
        };
        [self addSubview:_pickerView];
        
        //添加按钮
        UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, _pickerView.frame.size.height, frame.size.width/2, 50)];
        [closeBtn setTitle:@"取消" forState:UIControlStateNormal];
        [closeBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [closeBtn setBackgroundColor:[UIColor whiteColor]];
        closeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [closeBtn addTarget:self action:@selector(closeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width/2, _pickerView.frame.size.height, frame.size.width/2, 50)];
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn setBackgroundColor:[UIColor whiteColor]];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [sureBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:closeBtn];
        [self addSubview:sureBtn];
        
        maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [maskView addGestureRecognizer:tap];
        //添加中间分割线
        UIView *vc = [UIView new];
        vc.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:vc];
        
        [vc zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
            layout.xCenterByView(self,0);
            layout.widthValue(1);
            layout.topSpace(164);
            layout.bottomSpace(12);
        }];
    }
    return self;
}

#pragma mark -- 取消按钮
-(void)closeBtnAction:(UIButton *)btn {
    [self dismiss];
}

#pragma mark -- 确认按钮
-(void)sureBtnAction:(UIButton *)btn {
    if (self.sure_block) {
        self.sure_block(str_date);
    }
}

-(void)show {
    [self animationWithView:self duration:.3f];
    
    maskView.alpha = 0;
    
    [UIView animateWithDuration:.25f animations:^{
        maskView.alpha = 0.5;
    }];
    
    [[UIApplication sharedApplication].keyWindow addSubview:maskView];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    [view.layer addAnimation:animation forKey:nil];
}

-(void)dismiss {
    [maskView removeFromSuperview];
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
